package com.garen.fire.web;

import com.garen.fire.biz.IFireDutyBiz;
import com.garen.fire.entity.FireDuty;
import org.springframework.stereotype.Controller;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.garen.common.*;
import org.springframework.web.servlet.ModelAndView;

import com.garen.sys.entity.FireUser;
import com.garen.sys.filter.SysFilter;


@Controller
@RequestMapping("/fire")
public final class FireDutyAction extends BaseAction{

	//排班开始时间
	private final static int START_CLOCK = 8;
	
	@Autowired
	private IFireDutyBiz iFireDutyBiz;

	@RequestMapping("/fireduty/json") 
	public ModelAndView json(PageBean<FireDuty> pb,FireDuty fireDuty) {
		iFireDutyBiz.getFireDutyList(pb,fireDuty);
		return Json(pb);
	}
	
	// 获取指定中队当天值班人员
	@RequestMapping("/fireduty/jsonnow")
	public ModelAndView jsonNow(FireDuty fireDuty) {
		MsgBean mbean = SysFilter.getMsgBean();
		FireUser user = getSessionUser();
		if (fireDuty.getFireOrg() == null)
			fireDuty.setFireOrg(user.getUserOrgId());
		Calendar cale = Calendar.getInstance();
		if (fireDuty.getFireDate() != null)
			cale.setTime(fireDuty.getFireDate());
		//取系统时间
		int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
		Date kssj = null, jssj = null;
		log.debug("hour === " + hour);
		if (hour < START_CLOCK) {// 减一天
			cale.set(Calendar.HOUR_OF_DAY, START_CLOCK);
			jssj = cale.getTime();
			cale.add(Calendar.DAY_OF_YEAR, -1);
			kssj = cale.getTime();
		} else {
			cale.set(Calendar.HOUR_OF_DAY, START_CLOCK);
			kssj = cale.getTime();
			cale.add(Calendar.DAY_OF_YEAR, 1);
			jssj = cale.getTime();
		}
		mbean.setObj(iFireDutyBiz.getFireDutyList(fireDuty.getFireOrg() + "", kssj, jssj));
		return Json(mbean);
	}

	@RequestMapping("/fireduty/boxJson") 
	public ModelAndView boxJson(FireDuty fireDuty) {
		return Json(iFireDutyBiz.getFireDutyList(fireDuty));
	}

	@RequestMapping("/fireduty/edit") 
	public ModelAndView edit(ModelAndView mav,FireDuty fireDuty) {
		mav.setViewName("fire/FireDutyEdit");
		if(fireDuty.getDutyId() != null){
			fireDuty=iFireDutyBiz.getFireDuty(fireDuty);
		}
		mav.addObject("fireDuty",fireDuty);
		return mav;
	}

	@RequestMapping("/fireduty/save") 
	public ModelAndView save(FireDuty fireDuty) {
		MsgBean mbean = SysFilter.getMsgBean(0, "保存成功 !");
		iFireDutyBiz.saveFireDuty(mbean,fireDuty);
		return Json(mbean);
	}

	@RequestMapping("/fireduty/saveAll") 
	public ModelAndView saveAll(Date date,String dutyJsons) {
		FireUser user = getSessionUser();
		Calendar cale = Calendar.getInstance();
		if (date != null)
			cale.setTime(date);
		cale.set(Calendar.HOUR_OF_DAY, START_CLOCK);
		date = cale.getTime();
		MsgBean mbean = SysFilter.getMsgBean(0, "保存成功 !");
		List<FireDuty> dutyList = JSON.parseArray(dutyJsons, FireDuty.class);
		iFireDutyBiz.saveFireDutyAll(mbean,date,user.getUserOrgId(),dutyList);
		return Json(mbean);
	}

	@RequestMapping("/fireduty/del") 
	public ModelAndView del(FireDuty fireDuty) {
		MsgBean mbean = SysFilter.getMsgBean(0, "删除成功 !");
		iFireDutyBiz.delFireDuty(mbean,fireDuty);
		return Json(mbean);
	}

}
